//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Globals.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TKanaForm *KanaForm;
//---------------------------------------------------------------------------
__fastcall TKanaForm::TKanaForm(TComponent* Owner)
 : TForm(Owner)
{
}


//--------------------------------//
//                                //
//--------------------------------//
void __fastcall TKanaForm::ShowText( BYTE *String, bool flag )
{
  WORD val[4];
  BYTE *TextBuf = String;
  char buf[256];
  bool cwFirst;
  AnsiString buffer;

  //
  if ( flag ) kanaEdit->Clear( );
  buffer = "";
  //
//  if ( flag || txt.ptr_index == last_ptr_index ) return;
  if ( flag ) return;
//  if ( flag ) return;
  // We displayed it so display no more
  last_ptr_index = txt.ptr_index;
  kanaEdit->Clear( );
  //------------------------//
  //                        //
  //------------------------//
  kanaEdit->Lines->BeginUpdate( );

  txt.data = 0;
  while( txt.data != 0xFFFF )
  { //
    txt.data     = *(WORD *)&TextBuf[0];
    TextBuf += 1;

    //----------------//
    // End Text Write //
    //----------------//
    if ( txt.data == 0xFFFF )
    {
      break;
    }

    //-------------//
    // Control Codes    //
    //-------------//
    if ( (txt.data & 0xF000) == 0xF000 )
    { //
      TextBuf += 1;
      //
      switch( (txt.data & 0xFF00) )
      { // Keypress
        case 0xF000: sprintf( buf, "[Key]" ); break;
        // Pause for $xx ticks
        case 0xF100: sprintf( buf, "[Pause$%02X]", (txt.data & 0xFF) ); break;
        // ??
        case 0xF200: sprintf( buf, "[$F200]" ); break;
        // Make a sound
        case 0xF300: sprintf( buf, "[SFX]" ); break;
        // Player's name
        case 0xF400: sprintf( buf, "[Player]"); break;
        // Number at $A0,X
        case 0xF500: sprintf( buf, "[MEM_Num]"); break;
        // Memory Indexed Compression
        case 0xF600: sprintf( buf, "[MEM_Text]"); break;
        // Fade in
        case 0xF700: sprintf( buf, "[FadeIn]" ); break;
        // Fade Out
        case 0xF800: sprintf( buf, "[FadeOut]"); break;
        // Yes - Nox box
        case 0xF900: val[0]       = *(WORD *)&TextBuf[0];
                     val[1]       = *(WORD *)&TextBuf[2];
                     TextBuf += 4;
                     sprintf( buf, "[Yes/No$%04X-$%04X]", val[0], val[1] );
                     break;
        // ??
        case 0xFA00: sprintf( buf, "[$FA00]"  ); break;
        // Re-arrange TextBox
        case 0xFB00: sprintf( buf, "[FixTextBox]"); break;
        // Clear Screen
        case 0xFC00: //kanaEdit->Text = kanaEdit->Text +  "[ClrWin]" ;
                     //kanaEdit->Lines->Add( "" );
                     buffer = buffer +  "[ClrWin]" ;
                     cwFirst = 1;
//                     continue;
        // NewLine
        case 0xFD00: //kanaEdit->Lines->Add( "" ); continue;
                     buffer = buffer + ( "\r\n" );
                     continue;
        //--------------------------------
        default    : TextBuf--; break;
      }
      //
      buffer = buffer + AnsiString( buf );
    }
    else
    { // Standard text
      if ( duals[ ((txt.data & 0x00FF) << 8) + TextBuf[0] ].length )
//        kanaEdit->Text = kanaEdit->Text + AnsiString( (char *)duals[ (txt.data << 8) + TextBuf[0] ].text );
        buffer = buffer + AnsiString( (char *)duals[ (txt.data << 8) + TextBuf[0] ].text );
      else
//        kanaEdit->Text = kanaEdit->Text + AnsiString( "[$" + IntToHex(txt.data, 4 ) + "]" );
        buffer = buffer + AnsiString( "[$" + IntToHex(txt.data, 4 ) + "]" );
      //
      TextBuf += 1;
    }
  } // WEND

  txt.data       = 0;
  kanaEdit->Text = buffer;
  kanaEdit->Lines->EndUpdate( );
}

//--------------------------------//
//                                //
//--------------------------------//
void __fastcall TKanaForm::FormCreate(TObject *Sender)
{
  last_ptr_index = -1;
}

